﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FreightSoft.Models;

namespace FreightSoft.Controllers
{
    public class PartController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /Part/

        public ActionResult Index()
        {
            return View(db.PARTs.ToList());
        }

        //
        // GET: /Part/Details/5

        public ActionResult Details(decimal id = 0)
        {
            PART part = db.PARTs.Find(id);
            if (part == null)
            {
                return HttpNotFound();
            }
            return View(part);
        }

        //
        // GET: /Part/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Part/Create

        [HttpPost]
        public ActionResult Create(PART part)
        {
            if (ModelState.IsValid)
            {
                part.UNIT_COST = 10 * part.WEIGHT + 5 * part.VOLUME + 10;
                db.PARTs.Add(part);
                db.SaveChanges();
                return RedirectToAction("Index/" + part.PART_ID);
            }

            return View(part);
        }

        //
        // GET: /Part/Edit/5

        public ActionResult Edit(decimal id = 0)
        {
            PART part = db.PARTs.Find(id);
            if (part == null)
            {
                return HttpNotFound();
            }
            return View(part);
        }

        //
        // POST: /Part/Edit/5

        [HttpPost]
        public ActionResult Edit(PART part)
        {
            if (ModelState.IsValid)
            {
                db.Entry(part).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(part);
        }

        //
        // GET: /Part/Delete/5

        public ActionResult Delete(decimal id = 0)
        {
            PART part = db.PARTs.Find(id);
            if (part == null)
            {
                return HttpNotFound();
            }
            return View(part);
        }

        //
        // POST: /Part/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {
            PART part = db.PARTs.Find(id);
            db.PARTs.Remove(part);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}